package com.scholar.goods.controller;

import com.scholar.shop.common.BusinessException;
import com.scholar.shop.common.CommonRes;
import com.scholar.shop.common.EmBusinessError;
import com.scholar.shop.seller.specification.api.SpecificationApi;
import com.scholar.shop.seller.specification.domain.SpecificationOpenVO;
import com.scholar.shop.seller.specification.domain.SpecificationValuesOpenVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @author kimmy
 **/
@RestController
@RequestMapping("/seller/goods")
public class GoodsSpecController {

    private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(GoodsSpecController.class);

    /**
     * 商品规格API操作
     */
    @Autowired
    private SpecificationApi specificationApi;

    /**
     * 根据分类id查询规格包括规格值
     * @param categoryId 商品所属分类id
     * @return 商品分类对应的规格列表
     */
    @GetMapping("/categories/{categoryId}/specs")
    public CommonRes<List<SpecificationOpenVO>> sellerQuerySpec(@PathVariable("categoryId") String categoryId) throws Exception {

        if (StringUtils.isEmpty(categoryId)) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_IS_NULL);
        }

        return specificationApi.sellerQuerySpec(categoryId);
    }

    /**
     * 商家自定义某分类的规格项
     * @param categoryId 商品所属分类id
     * @param specName 规格名称
     * @return 结果
     */
    @PostMapping("/categories/{categoryId}/specs")
    public CommonRes<SpecificationOpenVO> add(@PathVariable("categoryId") String categoryId, String specName) throws Exception {

        if (StringUtils.isEmpty(categoryId)) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_IS_NULL);
        }

        if (StringUtils.isEmpty(specName)) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_IS_NULL);
        }

        return specificationApi.add(categoryId, specName);

    }

    /**
     * 商家自定义某规格的规格值
     * @param specId 规格id
     * @param valueName 规格值
     * @return 结果
     */
    @PostMapping("/specs/{specId}/values")
    public CommonRes<SpecificationValuesOpenVO> addValue(@PathVariable("specId") Integer specId, String valueName) throws Exception {
        if (StringUtils.isEmpty(specId)) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_IS_NULL);
        }

        if (StringUtils.isEmpty(valueName)) {
            throw new BusinessException(EmBusinessError.SYS_REQUEST_PARAMETER_IS_NULL);
        }

        return specificationApi.addValue(specId, valueName);
    }

}
